// 模拟readline
function stepRead(callback) {
  const input = process.stdin;
  const output = process.stdout;
  let line = '';
  emitKeyPressEvents(input);
  input.on('keypress', data => {
    output.write(data);
    line += data;
    switch (data) {
      case '\r\n': // 回车, mac上直接\r
        input.pause();
        callback(line);
        break;
    }
  });
  // 完全自己控制输入逻辑
  input.setRawMode(true);
  input.resume();
}


function emitKeyPressEvents(stream) {
  const g = emitKeys(stream);
  g.next();
  stream.on('data', chunk => {
    g.next(chunk.toString());
  });
}

function* emitKeys(stream) {
  while (true) {
    let ch = yield;
    stream.emit('keypress', ch);
  }
}

stepRead(data => {
  console.log('anwser:', data);
});

